<?php

namespace MLM\Model\Report;

use MLM\Model\Report;

class Storage extends \MLM\Model\Generic\Db\Storage{
    
    

   public function getSaldoMembers($accNum, $start, $limit, $searchField = "") {

        $sql = "select count(1) as total, FORMAT(sum(amount),2) as jumlah
               from accounts ";

        if ($accNum != ""){
            $sql .= " where account_num = '$accNum' ";
        }

        if ($searchField != ""){
            $sql .= " and $searchField ";
        }

        $stmt = $this->_db->query($sql);
        $getTotal = $stmt->execute();

        while($getTotal->next()){
                $res = $getTotal->current();
                $rows = $res['total'];
                $total = $res['jumlah'];
        }


        $data = array($rows, $total);


        $sql = "select *
                from accounts a
                left outer join bank b
                on b.bank_id = a.bank_id ";

        if ($accNum != ""){
            $sql .= " where a.account_num = '$accNum' ";
        }

        if ($searchField != ""){
            if ($accNum != "") $sql .= " and ";
            else $sql .= " where ";

            $sql .= " $searchField ";
        }

        $sql .= " order by a.account_num ";

        if ($limit != ""){
            $sql .= " limit $start, $limit";
        }

        $stmt = $this->_db->query($sql);
        $result = $stmt->execute();

        $dataBonus = array();
        while($result->next()){
                $res = $result->current();
                array_push($dataBonus, array("NoAkun" => $res['account_num'],
                                             "NamaAkun" => $res['account_name'],
                                             "Bank" => $res['bank_name'],
                                             "NoRek" => $res['bank_acc_no'],
                                             "Saldo" => $res['amount']));
        }


        array_push($data, $dataBonus);

        return $data;
    }

    
    
}
